
//  ButWhatMapView.m
//  BuyWhat
//
//  Created by ios on 2016/11/18.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "ButWhatMapView.h"
#import <CoreLocation/CoreLocation.h>
#import "UserModel.h"

@interface ButWhatMapView ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}

Copy void(^selectedHandle)();
@end

@implementation ButWhatMapView

- (instancetype)init{
    self = [super init];
    if(self){
        NSLog(@"初始化成功");
        _locationManager = [[CLLocationManager alloc]init];
        if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse)
        {
            [_locationManager requestWhenInUseAuthorization];
        }
        _locationManager.delegate = self;
        // 设置定位精度
        // kCLLocationAccuracyNearestTenMeters:精度10米
        // kCLLocationAccuracyHundredMeters:精度100 米
        // kCLLocationAccuracyKilometer:精度1000 米
        // kCLLocationAccuracyThreeKilometers:精度3000米
        // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
        // kCLLocationAccuracyBestForNavigation:导航情况下最高精度，一般要有外接电源时才能使用
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        
    }
    return self;
}


+ (ButWhatMapView*)sharedInstanced{
    static ButWhatMapView *_ButWhatMapView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _ButWhatMapView = [[ButWhatMapView alloc] init];
    });
    return _ButWhatMapView;
   
}

- (void)startUpdatingLocation:(void (^)())selectedHandle{
    _selectedHandle = selectedHandle;
     [_locationManager startUpdatingLocation];
}



// 地理位置发生改变时触发
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
    
    NSLog(@"经度%f,纬度%f",newLocation.coordinate.longitude, newLocation.coordinate.latitude);
    [[UserModel sharedInstanced]setlongitude:[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]];
    [[UserModel sharedInstanced]setlatitude:[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude]];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *place in placemarks) {
//            NSDictionary *location = [place addressDictionary];
//                        NSLog(@"国家：%@",[location objectForKey:@"Country"]);
//                        NSLog(@"城市：%@",[location objectForKey:@"State"]);
//                        NSLog(@"区：%@",[location objectForKey:@"SubLocality"]);
            NSLog(@"位置：%@",place.name);
            NSLog(@"国家：%@",place.country);
            NSLog(@"城市：%@",place.locality);
            [[UserModel sharedInstanced] setPlaceName:[NSString stringWithFormat:@"%@%@",place.locality,place.subLocality]];
          
            if (_selectedHandle) {
                _selectedHandle();
            }
            if (_getLocality) {
                _getLocality(place.locality);
            }
            [[UserModel sharedInstanced] setlocality:[NSString stringWithFormat:@"%@%@%@",place.locality,place.subLocality,place.name]];
            NSLog(@"%@",[UserModel sharedInstanced].locality);
            NSLog(@"区：%@",place.subLocality);
            NSLog(@"街道：%@",place.thoroughfare);
            NSLog(@"子街道：%@",place.subThoroughfare);
            
        }
    }];
    //一直定位
    [manager stopUpdatingLocation];
}



@end
